package com.example.door17;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Adaper extends RecyclerView.Adapter<Adaper.myHolder>{
private List<ContactModle> data;
private Context context;

    public Adaper(Context context) {
        this.context = context;
    }

    public void setData(List<ContactModle> data) {
        this.data = data;
    }

    @NonNull
    @Override
    public myHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
       //创建viewholder对象
        return new myHolder(LayoutInflater.from(context).
                inflate(R.layout.list_item_layout,null));
    }
    @Override
    public void onBindViewHolder(@NonNull  Adaper.myHolder holder, int position) {
        holder.contactid.setImageResource(data.get(position).getContactid());
        holder.contactname.setText(data.get(position).getContactname());
            //数组条目点击事件同
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"点击了"+data.get(position).getContactname(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.contactid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"点击了"+data.get(position).getContactname()+"的头像",Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return null != data? data.size() : 0;
    }
    class myHolder extends RecyclerView.ViewHolder {
        ImageView contactid;
        TextView contactname;
        public myHolder(@NonNull  View itemView) {
            super(itemView);
            contactid=itemView.findViewById(R.id.imge_ico);
            contactname=itemView.findViewById(R.id.text_tst);
        }
    }
}
